/*
作者：江怀玉
日期：2014-05-10
功能：数据统计分析功能
解决：快速搜索数据问题

-------------------------------------
依赖:
1,jQuery,
2,jQuery.Data
3,相关列表界面
4,样式文件

const_QueryField="RoleName"，快搜
const_DataFields="[{key:'RoleID',label:'RoleID',width:10,sortable:true,resizeable:true},{key:'iSun_Selected',label:'',width:10,formatter:"checkbox"},{key:'RoleName',label:'角色名称',width:100,sortable:true,resizeable:true},{key:'Remark',label:'描述',width:500,sortable:true,resizeable:true}]
---------------------------------------------------------*/
(function ($) {
    /*
    f:字段对象或快搜字段
    t:搜索类型
    callback:回调方法
    */
    $.fn.DataTotal = function (f, fs/*,t*/, callback) {
        var defaults = {
            field: f,                       //快搜字段
            datafields: fs,                 //字段列表
            //type:type,                   //查询类型
            operation: [{ Text: "包含", Value: "like" }, { Text: "等于", Value: " = " }, { Text: "大于", Value: ">" }, { Text: "大于且等于", Value: ">=" }, { Text: "小于", Value: "<" }, { Text: "小于且等于", Value: "<=" }, { Text: "不等于", Value: "<>" }, { Text: "不包含", Value: "not like" }, { Text: "在.#.之间", Value: "between"}],
            fn: [{ Text: "求合", Value: "SUM" }, { Text: "最大值", Value: "Max" }, { Text: "最小值", Value: "MIN" }, { Text: "求平均", Value: "Avg" }, { Text: "记录数", Value: "Count" }, { Text: "分组", Value: "Group"}],
            orand: [{ Text: " ", Value: " " }, { Text: "与", Value: "and" }, { Text: "或", Value: "or"}]
        };
        $(this).click(HightDialogBulider);
        //高级查询界面
        function HightDialogBulider() {
            //debugger
            var data = fnFormatQueryTable();
            var X = $(this).offset().top;
            var Y = $(this).offset().left;
            //$("#div_gridshow").html("");
            if ($("#div_list_DataTotal").attr("id") == undefined) {
                $('body').append('<div id="div_list_DataTotal" style="overflow: auto;z-index: 1000;border: 1px solid #A8A8A8;border-top: 1px solid #A8A8A8;width:400px;height:300px;position: absolute; background-color: #fff; display: none;">' +
                '<div style="height:1px;width:500px; background-color: #6699cc;"></div>' +
                '<div style="height:25px; margin-bottom:0px;width:500px; background-color:  #6699cc;">' +
                '<div style="height:242px;widht:500px;" id="div_list_DataTotal_data"></div>' +
                '<input type="button" id="btn_Query_OK" value="查询">' +
                '<input type="button"  style="margin-right:0px;" id="btn_Query_Empty_1" value="清空">' +
                '<input type="button"  style="margin-right:0px;" id="btn_Query_Cancel_1" value="关闭">' +
                '<input type="button"  style="margin-right:0px;" id="btn_Query_Hight_1" value="高级查询">' +
                '</div>');
                $("#div_list_DataTotal").height(300);
                $("#div_list_DataTotal").width(500);
                var sbhtml = '<table class="querytable" style="widht:500px"><thead><th>字段</th><th>操作</th><th>值</th><th>函数</th><</thead>';
                if (data != "") {
                    sbhtml += '<tbody>' + data + '</tbody>';
                } else {
                    sbhtml += '<tbody><tr><td style="color:red;text-align:center;width:500px;" colspan=4>没有定义列</td></tr></tbody>';
                }
                $("#div_list_DataTotal_data").html(sbhtml);
                $("#btn_Query_OK1").click(function () {
                    var condition = getCondtionStr();
                    if (condition != "") {
                        $("#div_list_DataTotal").hide();
                    }
                    else {
                        alert("请输入值进行搜索");
                    }
                }); 
                $("#btn_Query_Cancel1").click(function () {
                    $("#div_list_DataTotal").hide();
                });
                $("#btn_Query_Empty1").click(function () {
                    $("#div_list_DataTotal input").val("");
                });
                $("#btn_list_Quick_Query").click(function () {
                    var condition = fnQuciklyQuery();
                    if (condition != "") {
                        alert(condition);
                        $("#div_list_DataTotal").hide();
                    }
                    else {
                        alert("请输入值进行搜索");
                    }
                });
            }
            $("#div_list_DataTotal").css("left", Y).css("top", X + 25).show();
        }


        function getCondtionStr() {
            var $tr = $("#div_list_DataTotal table tbody tr");
            var _c = ""; //条件
            $tr.each(function (e) {
                var _v = $(this).find("input")[0].value;
                if (_v != "") {
                    var _f = $(this).attr("id").replace("tr_", "");
                    var _o = $($(this).find("select")[0]).val();
                    var arrayc = new Array();              //值
                    arrayc = _v.split('#');
                    _v = arrayc.length > 1 ? ("'" + arrayc[0] + "' and '" + arrayc[1] + "'") : (_o.indexOf("like") > 0 ? ("'%" + arrayc[0] + "%'") : ("'" + arrayc[0] + "'"));
                    _s = _f + " " + _o + " " + _v;
                    _c = _c == "" ? _s : _c + " and " + _s;
                }
            });
            return _c;
            //alert(_c);
        }



        var fnFormatQueryTable = function () {
            var sehtml = fnFormatSelectOption();
            var sefnhtml = fnFormatSelectFnOption();
            var sbhtml = "";
            for (i = 2; i < defaults.datafields.length; i++) {
                sbhtml = sbhtml + '<tr id="tr_' + defaults.datafields[i].key + '"><td style="width:115px">' + defaults.datafields[i].label + '</td><td>' + sehtml + '</td><td><input style="width:200px"><td><td>' + sefnhtml + '</td></tr>';
            
            }
            return sbhtml;
        }

        var fnFormatSelectOption = function () {
            var str1 = [];
            str1.push("<select style='width:80px'>");
            for (i = 0; i < defaults.operation.length; i++) {

                str1.push("<option value='" + defaults.operation[i].Value + "'>" + defaults.operation[i].Text + "</option>");
            }
            str1.push("</select>");
            return str1.join("");
        }

        var fnFormatSelectFnOption = function () {
            var str1 = [];
            str1.push("<select style='width:80px'>");
            for (i = 0; i < defaults.fn.length; i++) {

                str1.push("<option value='" + defaults.fn[i].Value + "'>" + defaults.fn[i].Text + "</option>");
            }
            str1.push("</select>");
            return str1.join("");
        }

        //快搜生成&一般查询
        function QuickDialogBulider() {

        }
    }
})(jQuery);



